package com.oe.market.modules.ele.model.dao;

import com.oe.market.modules.common.model.ADaoBase;
import com.oe.market.modules.ele.model.bean.Cookie;
import com.oe.market.modules.ele.model.bean.CookieUseCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class CookieUseCountDao extends ADaoBase<CookieUseCount> {

    private static final Logger log = LoggerFactory.getLogger(CookieUseCountDao.class);
    public final static CookieUseCountDao me = new CookieUseCountDao();

    public List<CookieUseCount> searchValid(Integer accountId) {
        String sql = "select cookie_id, today_use, avatar, nickname, create_time, status, sns_hash from " + getTableName() + " where from_account_id=" + accountId + " and `status`=0;";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<CookieUseCount> findByPhone(String phone) {
        String sql = "select `status` from " + getTableName() + " where phone='" + phone + "';";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }
    public List<CookieUseCount> findBySnsHash(String snsHash) {
        String sql = "select * from " + getTableName() + " where sns_hash='" + snsHash + "';";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }


    public List<CookieUseCount> findByAccount(Integer accountId) {
        String sql = "select * from " + getTableName() + " where from_account_id=" + accountId + ";";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }


    public String useOnce(String snsHash) {
        try {
            String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("sns_hash",(snsHash));
            List<CookieUseCount> cookieUseCounts = find(sql);
            if (0 == cookieUseCounts.size())
                return "该账户不存在";
            CookieUseCount cookieUseCount = cookieUseCounts.get(0);

            //////////////////////////////////////////////

            Integer use = cookieUseCount.getUse() + 1;
            Integer todayUse = cookieUseCount.getTodayUse() + 1;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
            sql = "UPDATE `" + getTableName() + "` SET `use`=" + use
                    +", `today_use`=" + todayUse + ", `modify_time`='" + dateFormat.format(new Date())
                    + "' WHERE (`sns_hash`='"+ snsHash + "');";

            int ret = executeSql(sql);
            if (0 == ret) {
                return "更新账户信息失败";
            }

        } catch (SQLException e) {
            log.error(snsHash);
            log.error(e.getMessage());
            return "未知错误";
        }
        return null;
    }

    public List<CookieUseCount> findByAccountAndPhone(Integer accountId, String phone) {
        String sql = "select * from " + getTableName() + " where from_account_id=" + accountId
                + " and phone='"+ phone +"';";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }
    public List<CookieUseCount> searchUse() {
        String sql = "select * from " + getTableName() + " where today_use<5 AND max_use_day > today_use;";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public boolean ban(Integer cookieId) {
        String sql = "UPDATE `" + getTableName() + "` SET `status`= 3 WHERE (`cookie_id`='"+ cookieId + "');";
        if (null == update(sql)) {
            return true;
        }
        return false;
    }

    public boolean banInfoId(Integer infoId) {
        String sql = "UPDATE `" + getTableName() + "` SET `status`= 3 WHERE (`info_id`='"+ infoId + "');";
        if (null == update(sql)) {
            return true;
        }
        return false;
    }




    ////////////////////////
    @Override
    public CookieUseCount getThisClassInstance() {
        return new CookieUseCount();
    }

    @Override
    public String getTableName() {
        return "oeasy_ele_cookie_use_count";
    }




}
